﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;

namespace UE.SharePoint.ImportExport.BLL
{
    /// <summary>
    /// Represents a common function when the sharepoint runtime.
    /// </summary>
    public static class Util
    {
        /// <summary>
        /// Converts the specified string value to equivalent int number
        /// </summary>
        /// <param name="oriStr">origanal value</param>
        /// <param name="defaultValue">default value</param>
        /// <returns></returns>
        public static int StrToInt(string oriStr, int defaultValue = 0)
        {
            if (string.IsNullOrEmpty(oriStr) || oriStr == "")
            {
                return defaultValue;
            }

            return int.Parse(oriStr);
        }  

        /// <summary> 
        /// Used to test runtime time of specified function 
        /// </summary>
        /// <param name="action">function</param>
        /// <param name="repeatCount">Repeat times</param>
        /// <returns></returns>
        public static TimeSpan Benchmark(Action action, int repeatCount = 1)
        {
            var start = DateTime.Now;
            for (int i = 0; i < repeatCount; i++)
            {
                action.Invoke();
            }
            var end = DateTime.Now;

            return end - start;
        } 

        /// <summary>
        /// Write log to uls
        /// </summary>
        /// <param name="ex"></param>
        /// <param name="title"></param>
        /// <returns></returns>
        public static string WriteULSLog(this Exception ex, string title)
        {
            return WriteULSLog(title, ex.GetDetail(),TraceSeverity.Unexpected);
        }

        /// <summary>
        /// Write log to uls
        /// </summary>
        /// <param name="title"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static string WriteULSLog(string title, string msg, TraceSeverity traceSeverity = TraceSeverity.Medium)
        {
            try
            {
                Debug.WriteLine(msg);
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPDiagnosticsCategory category = new SPDiagnosticsCategory(title,TraceSeverity.Medium, EventSeverity.Information);
                    SPDiagnosticsService ds = SPDiagnosticsService.Local;
                    ds.WriteTrace(10000, category, traceSeverity, msg); 
                });
            }
            catch
            {
            }
            return msg;
        }

        /// <summary>
        /// Write log to uls
        /// </summary>
        /// <param name="ex"></param>
        /// <returns></returns>
        public static string WriteULSLog(this Exception ex)
        {
            return WriteULSLog(ex.GetDetail());
        }

        /// <summary>
        /// Write log to uls
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="args"></param>
        public static void WriteULSLog(string msg, params object[] args)
        {
            WriteULSLog(String.Format(msg, args));
        }

        /// <summary>
        /// Write log to uls
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static string WriteULSLog(string msg)
        {
            WriteULSLog("UE.SharePoint.SharePoint", msg); return msg;
        } 

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ex"></param>
        /// <returns></returns>
        public static string GetDetail(this Exception ex)
        {
            StringBuilder msg = new StringBuilder();
            msg.AppendLine(ex.Message)
                .AppendLine(ex.StackTrace);

            Exception cMsg = ex.InnerException;

            while (cMsg != null)
            {
                msg.AppendLine(cMsg.Message).AppendLine(cMsg.StackTrace);

                cMsg = cMsg.InnerException;
            }

            return msg.ToString();
        }
    }
}
